package com.linck.im.client.controller;

import com.linck.im.client.context.AccountContext;
import com.linck.im.client.context.ChannelContext;
import com.linck.im.client.context.ChatContext;
import com.linck.im.client.context.ControllerContext;
import com.linck.im.model.proto.ProtoMsg;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: linck
 * @create: 2021-08-07
 */
public class ChatController implements Initializable {

    public ChatController() {ControllerContext.putController(this.getClass(), this);}

    @FXML
    private ListView friendListView;

    @FXML
    private TextArea chatArea;

    @FXML
    private TextField messageTextField;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (Objects.nonNull(AccountContext.accountList)) {
            ChatController.setFriendListView(AccountContext.accountList.stream().map(t -> t.getAccount()).collect(Collectors.toList()));
        }
        friendListView.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> {
                    AccountContext.toAccount = (String) newValue;
                    instanceUpdateChatView((String) newValue);
                }
        );
    }

    public static void updateChatView(String account) {
        ChatController chatController = (ChatController) ControllerContext.getController(ChatController.class);
        Platform.runLater(() -> {
            if (Objects.nonNull(chatController)) {
                chatController.instanceUpdateChatView(account);
            }
        });
    }

    private void instanceUpdateChatView(String account) {
        List<ProtoMsg.Message> messages = ChatContext.getMessages(account);
        StringJoiner joiner = new StringJoiner("\n");
        for (ProtoMsg.Message message : messages) {
            joiner.add(message.getFrom()+":"+message.getContent());
        }
        chatArea.setText(joiner.toString());
    }

    public void onSendButtonClick() {
        String message = messageTextField.getText();
        ProtoMsg.Message msg = ProtoMsg.Message.newBuilder()
                .setFrom(AccountContext.account)
                .setTo(AccountContext.toAccount)
                .setContent(message)
                .setTime(System.currentTimeMillis())
                .build();
        ChatContext.sendMessage(msg);
        ChannelContext.getChannel().writeAndFlush(msg);
        instanceUpdateChatView(AccountContext.toAccount);
        messageTextField.setText("");
    }

    public static void setFriendListView(List<String> accounts) {
        ChatController chatController = (ChatController) ControllerContext.getController(ChatController.class);
        Platform.runLater(() -> {
            if (Objects.nonNull(chatController)) {
                chatController.instanceSetFriendListView(accounts);
            }
        });
    }

    public void instanceSetFriendListView(List<String> accounts) {
        ObservableList<String> userList = FXCollections.observableArrayList();
        accounts.forEach(account -> userList.add(account));
        friendListView.setItems(userList);
    }
}
